 class ListNode {
     int val;
     ListNode next;
     ListNode() {}
     ListNode(int val) { this.val = val; }
     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}

public class Main {
    public static ListNode middleNode(ListNode head) {
        ListNode cur = head;
        int n = Size(head);
        for(int i = 0; i< n/2;i++){
            cur = cur.next;
        }
        return cur;
    }
    public static int Size(ListNode head){
        ListNode pc = head;
        int size = 0;
        while(pc != null){
            size++;
            pc = pc.next;
        }
        return size;
    }

    public static void main(String[] args) {
        // 测试用例4：三个节点的链表
        ListNode head4 = new ListNode(1, new ListNode(2, new ListNode(3)));
        ListNode middle4 = middleNode(head4);
        System.out.println("测试用例4：三个节点的链表，中间节点为：" + middle4.val);

        // 测试用例5：四个节点的链表
        ListNode head5 = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4))));
        ListNode middle5 = middleNode(head5);
        System.out.println("测试用例5：四个节点的链表，中间节点为：" + middle5.val);
    }
}
